#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <linux/input.h>


/* 定义一个 input_event 变量，存放输入事件信息 */
static struct input_event inputevent;

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd, ret;
	char *filename;

	if(argc != 2){
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	/* 打开key驱动 */
	fd = open(filename, O_RDWR);
	if(fd < 0)
	{
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	/* 循环读取按键值数据！ */
	while(1) 
	{
		ret = read(fd, &inputevent, sizeof(inputevent));
		if (ret <= 0)
		{
			printf("data unvalid.\r\n");
		}
		else 
		{
			//printf("type=%d,code= %d\r\n", inputevent.type, inputevent.code);
			switch (inputevent.type)
			{
			case EV_KEY:
				if (inputevent.code < BTN_MISC) { /* 键盘键值 */
					printf("key %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
				}
				else
				{
					printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
				}
				break;
			case EV_REL:
				break;
			case EV_ABS:
				break;
			case EV_MSC:
				break;
			case EV_SW:
				break;
			default:
				break;
			}
		}
	}

	ret= close(fd); /* 关闭文件 */
	if(ret < 0)
	{
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}
